Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: access-api uses DID env variable when building its ucanto server id #275

Merged
merged 9 commits into from
Dec 12, 2022

Conversation

gobengo
Copy link
Contributor

@gobengo gobengo commented Dec 8, 2022

Motivation:

Notes

  • This is intended to be safe to merge/deploy as is. The new functionality can be opted-into via env vars
  • The ucanto server will only be constructed with an opts.id that is a signer withDID(...) iff process.env.DID is set to a DID
    • If process.env.DID is unset, the ucanto server id signer will have a did() corresponding to config.PRIVATE_KEY (it will be a did:key of the corresponding public key)
  • Before this PR, many tests of the access-api worker were configured implicitly via dotenv with whatever the /.env.local file happened to contain. I added a worker test that asserts functionality that is dependent on the details of the env vars, so I made an affordance for tests to be able to configure the access-api worker with environment variables defined in the test case itself.

@gobengo gobengo temporarily deployed to dev December 8, 2022 22:58 — with GitHub Actions Inactive
@gobengo gobengo changed the title access-api uses DID env variable when building its ucanto server id feat: access-api uses DID env variable when building its ucanto server id Dec 8, 2022
@gobengo gobengo temporarily deployed to dev December 8, 2022 23:06 — with GitHub Actions Inactive
Copy link
Contributor

@Gozala Gozala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Thanks

export function configureSigner(config) {
const signer = Signer.parse(config.PRIVATE_KEY)
if (config.DID) {
if (!isDID(config.DID)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it may be better to use existing functionality for this, for example this will parse and throw if it's invalid:

DID.match({ method: 'web' }).from(config.DID)

It will also take care of refusing did:key which would be invalid in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gobengo gobengo temporarily deployed to dev December 8, 2022 23:51 — with GitHub Actions Inactive
if (parts[0] !== 'did') return false
return true
try {
return Boolean(DID.match({}).from(object))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What’s the point of catching here to then throw ? You can simply just do .from instead of whole isDID thing.

Alternatively there’s also .is method which you could instead which will return boolean

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 5184a55

@@ -42,12 +41,12 @@ export function getContext(request, env, ctx) {
env: config.ENV,
})

const keypair = Signer.parse(config.PRIVATE_KEY)
const signer = configureSigner(config)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you do this inside the loadConfig ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 b9533c9

const url = new URL(request.url)
const db = new D1QB(config.DB)
return {
log,
signer: keypair,
signer,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
signer,
signer: Signer.parse(config.PRIVATE_KEY).withDID(config.ENV === 'production' ? 'did:web:ucan.web3.storage': `did:web:ucan-${config.ENV}.web3.storage`),

can't we just do this ? any error will be caught at dev time or CI.

Copy link
Contributor Author

@gobengo gobengo Dec 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want the advertised DID to come from env variable. A '.web3.storage' domain won't be the best choice 100% of the times, e.g. on localhost or testing various scenarios. env variable gives me flexibility to get something working. once something works/tests end to end, can narrow down the configurability.

environment: {
...process.env,
PRIVATE_KEY:
'MgCYWjE6vp0cn3amPan2xPO+f6EZ3I+KwuN1w2vx57vpJ9O0Bn4ci4jn8itwc121ujm7lDHkCW24LuKfZwIdmsifVysY=',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need a new key here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't. Good catch. 🙏 d204331

@gobengo gobengo temporarily deployed to dev December 9, 2022 17:40 — with GitHub Actions Inactive
@gobengo gobengo temporarily deployed to dev December 9, 2022 17:45 — with GitHub Actions Inactive
@gobengo gobengo temporarily deployed to dev December 9, 2022 17:48 — with GitHub Actions Inactive
@@ -49,6 +55,9 @@ export function loadConfig(env) {
// eslint-disable-next-line no-undef
COMMITHASH: ACCOUNT_COMMITHASH,

DID,
signer,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we get the did:key and did:web from the signer instance ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

console.log('signer did', signer.did().toString())

signer did did:web:exampe.com

console.log('signer archive', signer.toArchive())

signer archive {
id: 'did:web:exampe.com',
keys: {
'did:key:z6MkqBzPG7oNu7At8fktasQuS7QR7Tj7CujaijPMAgzdmAxD': Ed25519Signer(68) [Uint8Array] [
128, 38, 22, 140, 78, 175, 167, 71, 39, 221, 169,
143, 106, 125, 177, 60, 239, 159, 232, 70, 119, 35,
226, 176, 184, 221, 112, 218, 252, 121, 238, 250, 73,
244, 237, 1, 159, 135, 34, 226, 57, 252, 138, 220,
28, 215, 109, 110, 142, 110, 229, 12, 121, 2, 91,
110, 11, 184, 167, 217, 192, 135, 102, 178, 39, 213,
202, 198
]
}
}

return {
DEBUG: boolValue(vars.DEBUG),
ENV: parseRuntimeEnv(vars.ENV),

PRIVATE_KEY: vars.PRIVATE_KEY,
PRIVATE_KEY,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we probably dont need this one anymore right ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gobengo gobengo temporarily deployed to dev December 9, 2022 23:19 — with GitHub Actions Inactive
Copy link
Contributor

@hugomrdias hugomrdias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@gobengo gobengo merged commit 311da78 into main Dec 12, 2022
@gobengo gobengo deleted the issues/237-access-api-didweb branch December 12, 2022 18:21
@gobengo gobengo self-assigned this Dec 12, 2022
hugomrdias added a commit that referenced this pull request Dec 13, 2022
🤖 I have created a release *beep* *boop*
---


##
[4.0.0](access-api-v3.0.0...access-api-v4.0.0)
(2022-12-13)


### ⚠ BREAKING CHANGES

* upgrade access-api @ucanto/* and @ipld/dag-ucan major versions
([#246](#246))
* access-client store decoupling
([#228](#228))
* upgrade to `@ucanto/{interface,principal}`@^4.0.0
([#238](#238))
* follow up on the capabilities extract
([#239](#239))
* doc capabilities & make requierd nb non-optionals
([#159](#159))
* Remove 0.8 caps and add account delegation to the service
([#123](#123))
* bump to 0.9
([#116](#116))

### Features

* [#153](#153)
([#177](#177))
([d6d448c](d6d448c))
* access-api uses DID env variable when building its ucanto server id
([#275](#275))
([311da78](311da78))
* **access-client:** cli and recover
([#207](#207))
([adb3a8d](adb3a8d))
* account recover with email
([#149](#149))
([6c659ba](6c659ba))
* bump to 0.9
([#116](#116))
([3e0b63f](3e0b63f))
* doc capabilities & make requierd nb non-optionals
([#159](#159))
([6496773](6496773))
* follow up on the capabilities extract
([#239](#239))
([ef5e779](ef5e779))
* Remove 0.8 caps and add account delegation to the service
([#123](#123))
([878f8c9](878f8c9)),
closes [#117](#117)
[#121](#121)
* Revert "feat!: upgrade to `@ucanto/{interface,principal}`@^4.0.0"
([#245](#245))
([c182bbe](c182bbe))
* sync encode/decode delegations
([#276](#276))
([ab981fb](ab981fb))
* upgrade access-api @ucanto/* and @ipld/dag-ucan major versions
([#246](#246))
([5e663d1](5e663d1))
* upgrade to `@ucanto/{interface,principal}`@^4.0.0
([#238](#238))
([2f3bab8](2f3bab8))


### Bug Fixes

* 0.9 ([#78](#78))
([1b1ed01](1b1ed01))
* add validation copy
([#257](#257))
([7f50af4](7f50af4)),
closes [#139](#139)
* fix Access API cannot get space/info
[#243](#243)
([#255](#255))
([1bacd54](1bacd54))
* fix d1 migrations
([#264](#264))
([fb8c09d](fb8c09d))
* make d1 spaces.metadata nullable and change to kysely
([#284](#284))
([c8a9ce5](c8a9ce5)),
closes [#280](#280)
* make multiformats 9 go away
([#133](#133))
([2668880](2668880))
* miniflare dev script
([#137](#137))
([1c5a4e2](1c5a4e2))
* testing staging deploy
([3d500fa](3d500fa))
* try to deploy api staging
([18b7b29](18b7b29))
* try to deploy staging
([c616818](c616818))


### Code Refactoring

* access-client store decoupling
([#228](#228))
([a785278](a785278))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
gobengo added a commit that referenced this pull request Apr 11, 2023
…r id (#275)

Motivation:
* #237

Notes
* This is intended to be safe to merge/deploy as is. The new
functionality can be opted-into via env vars
* The ucanto server will only be constructed with an opts.id that is a
signer `withDID(...)` iff `process.env.DID` is set to a DID
* If `process.env.DID` is unset, the ucanto server id signer will have a
`did()` corresponding to `config.PRIVATE_KEY` (it will be a `did:key` of
the corresponding public key)
* Before this PR, many tests of the access-api worker were configured
implicitly via dotenv with whatever the `/.env.local` file happened to
contain. I added a worker test that asserts functionality that is
dependent on the details of the env vars, so I made an affordance for
[tests to be able to configure the access-api worker with environment
variables defined in the test case
itself](https://github.com/web3-storage/w3protocol/pull/275/files#diff-e20ffc550d006747790e7b67da280446c1cd1d2d4f72ccb5df04f62cbb6423daR149).
gobengo pushed a commit that referenced this pull request Apr 11, 2023
🤖 I have created a release *beep* *boop*
---


##
[4.0.0](access-api-v3.0.0...access-api-v4.0.0)
(2022-12-13)


### ⚠ BREAKING CHANGES

* upgrade access-api @ucanto/* and @ipld/dag-ucan major versions
([#246](#246))
* access-client store decoupling
([#228](#228))
* upgrade to `@ucanto/{interface,principal}`@^4.0.0
([#238](#238))
* follow up on the capabilities extract
([#239](#239))
* doc capabilities & make requierd nb non-optionals
([#159](#159))
* Remove 0.8 caps and add account delegation to the service
([#123](#123))
* bump to 0.9
([#116](#116))

### Features

* [#153](#153)
([#177](#177))
([7fbf2a1](7fbf2a1))
* access-api uses DID env variable when building its ucanto server id
([#275](#275))
([e8326ec](e8326ec))
* **access-client:** cli and recover
([#207](#207))
([720dafb](720dafb))
* account recover with email
([#149](#149))
([91ad47d](91ad47d))
* bump to 0.9
([#116](#116))
([29cf63c](29cf63c))
* doc capabilities & make requierd nb non-optionals
([#159](#159))
([f6b6d06](f6b6d06))
* follow up on the capabilities extract
([#239](#239))
([717fcaa](717fcaa))
* Remove 0.8 caps and add account delegation to the service
([#123](#123))
([c3c58b9](c3c58b9)),
closes [#117](#117)
[#121](#121)
* Revert "feat!: upgrade to `@ucanto/{interface,principal}`@^4.0.0"
([#245](#245))
([197439e](197439e))
* sync encode/decode delegations
([#276](#276))
([9d48372](9d48372))
* upgrade access-api @ucanto/* and @ipld/dag-ucan major versions
([#246](#246))
([65d191c](65d191c))
* upgrade to `@ucanto/{interface,principal}`@^4.0.0
([#238](#238))
([309aff0](309aff0))


### Bug Fixes

* 0.9 ([#78](#78))
([561c68b](561c68b))
* add validation copy
([#257](#257))
([0bd49ce](0bd49ce)),
closes [#139](#139)
* fix Access API cannot get space/info
[#243](#243)
([#255](#255))
([1a74031](1a74031))
* fix d1 migrations
([#264](#264))
([5b8a6e7](5b8a6e7))
* make d1 spaces.metadata nullable and change to kysely
([#284](#284))
([7f09479](7f09479)),
closes [#280](#280)
* make multiformats 9 go away
([#133](#133))
([cdb4109](cdb4109))
* miniflare dev script
([#137](#137))
([f2cffb2](f2cffb2))
* testing staging deploy
([975cffc](975cffc))
* try to deploy api staging
([5130464](5130464))
* try to deploy staging
([4e56657](4e56657))


### Code Refactoring

* access-client store decoupling
([#228](#228))
([64c7496](64c7496))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants